<?php
namespace app\admin\controller;
use \think\Controller;
use think\Request;
use think\Session;
class Customs extends Controller
{
    public function _initialize(){
        if(!isLogin()){
            $this->redirect('/trade/login');
        }
    }
    public function index(Request $request)
    {
        //当前登录用户的id
        $id=Session::get('trade_user_id');
        $query=$request->param();
        $list=null;
        $account=null;
        $user=null;

        if(count($query)>0){
            //有搜索内容

            //只有在有参数时查询
            $account=$request->param('account') ? $request->param('account') : 'not null';
            $user=$request->param('user') ? $request->param('user') : 'not null';

            if($id==1){
                $list=db('custom')
                    ->where('user',trims($user))
                    ->where('account',trims($account))
                    ->paginate(20,false,['query'=>$query]);
            }else{
                $childId=array();
                $childId[]=$id;
                $childId[]= db('user')->where('id',$id)->value('pid');

                $list=db('custom')
                    ->where(['id'=>['in',$childId]])
                    ->where('user',trims($user))
                    ->where('account',trims($account))
                    ->paginate(20,false,['query'=>$query]);
            }

        }else{
            //无搜索
            if($id==1){
                $list=db('custom')->order('id')->paginate(20);
            }else{
                $childId=array();
                $childId[]=$id;
                $childId[]= db('user')->where('id',$id)->value('pid');

                $list=db('custom')->where(['id'=>['in',$childId]])->paginate(20);
            }
        }

        $this->assign('custom',$list);
        $this->assign('account',$request->param('account'));
        $this->assign('user',$request->param('user'));
        return $this->fetch('list');
    }


}
